var baseUrl = require('../../utils/urllink.js')

var app = getApp()
Page({
  data: {
    showTopTips: false,
    showTopTipsText: '',

    isAgree: false,
    files: [],
    files2: [],

    shop_id: 0,
    shop_name:'',
    shop_desc:'',
    shop_phone:'',
    shop_qq:'',
    seller_name:'',
    shop_logo:'',
    shop_avatar:'',

    hostimgs: app.globalData.hostimgs,
  },

  onLoad() {
    var that = this
    //获取店铺信息
    getshop(that, app.globalData.user_id)
  },
  onShow: function () {
    var that = this
    //获取店铺信息
    //getshop(that, app.globalData.user_id)
  },



  //店铺 表单提交
  formSubmit: function (e) {
    var that = this;
    var shop_id = that.data.shop_id;
    var shop_name = e.detail.value.shop_name;
    var shop_desc = e.detail.value.shop_desc;
    var shop_phone = e.detail.value.shop_phone;
    var shop_qq = e.detail.value.shop_qq;
    var seller_name = e.detail.value.seller_name;

    if (shop_name.length <= 0) {
      this.setData({
        showTopTipsText: '请输入店铺名称',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (shop_desc.length <= 0) {
      this.setData({
        showTopTipsText: '请输入店铺介绍',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (seller_name.length <= 0) {
      this.setData({
        showTopTipsText: '请输入联系人',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (shop_phone.length <= 0) {
      this.setData({
        showTopTipsText: '请输入手机号码',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {

      //店铺数据提交
      modi_shop_data(that, app.globalData.user_id, shop_id, shop_name, shop_desc, shop_phone, shop_qq, seller_name, that.data.shop_avatar, that.data.shop_logo)
    }






  },

  //上传图片 显示功能
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: [],
        })
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        })
        sendPhotos(res.tempFilePaths, that, app.globalData.openid, app.globalData.user_id, 1)
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  chooseImage2: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files2: [],
        })
        that.setData({
          files2: that.data.files2.concat(res.tempFilePaths)
        })
        sendPhotos(res.tempFilePaths, that, app.globalData.openid, app.globalData.user_id, 2)
      }
    })
  },
  previewImage2: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files2 // 需要预览的图片http链接列表
    })
  },





});



//上传图片
function sendPhotos(arr, that, openid, user_id, images_id) {
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  })

  wx.uploadFile({
    url: baseUrl + 'weapp/SavePicApi/save_shoppic.html',
    filePath: arr[0],
    name: 'file',
    formData: {
      //和服务器约定的token, 一般也可以放在header中
      'openid': openid,
      'user_id': user_id
    },
    header: { "Content-Type": "multipart/form-data" },
    success: function (res) {
      console.log(JSON.parse(res.data));
      if (images_id == 1) {
        var images1 = JSON.parse(res.data);
        that.setData({
          shop_logo: images1,
        })
      }
      if (images_id == 2) {
        var images2 = JSON.parse(res.data);
        that.setData({
          shop_avatar: images2,
        })
      }
      wx.showToast({
        icon: "success",
        title: "上传成功"
      })

    }

  })



}

// 获取店铺信息 (有店铺信息 返回信息，没有店铺 返回)
function getshop(that,user_id){
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Shop/shop_data.html',
    data: {
      user_id: user_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(res.data.code == -1) {
        that.setData({
          shop_id:0,
        })
      }else{
        getApp().globalData.shop_id = res.data.shop_id;
        //返回数据值
        that.setData({
          shop_id: res.data.shop_id,
          shop_name: res.data.shop_name,
          shop_desc: res.data.shop_desc,
          shop_phone: res.data.shop_phone,
          shop_qq: res.data.shop_qq,
          seller_name: res.data.seller_name,

          shop_logo: res.data.shop_logo,
          shop_avatar: res.data.shop_avatar,

        })
        if (!(res.data.shop_logo == null || res.data.shop_logo == '')) {
          that.setData({
            files: [],
          })
          that.setData({
            files: that.data.files.concat(that.data.hostimgs + res.data.shop_logo),
          })
        }
        if (!(res.data.shop_avatar == null || res.data.shop_avatar == '')) {
          that.setData({
            files2: [],
          })
          that.setData({
            files2: that.data.files2.concat(that.data.hostimgs + res.data.shop_avatar),
          })
        }
      }
    },
    fail: function () {},
    complete: function () {}
  })
}

//保存店铺数据 提交
function modi_shop_data(that, user_id, shop_id, shop_name, shop_desc, shop_phone, shop_qq, seller_name, shop_avatar, shop_logo){
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Shop/modi_shop_data.html',
    data: {
      user_id: app.globalData.user_id,
      shop_id: shop_id,
      shop_name: shop_name,
      shop_desc: shop_desc,
      shop_phone: shop_phone,
      shop_qq: shop_qq,
      seller_name:seller_name,
      shop_avatar: shop_avatar,
      shop_logo: shop_logo,

    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == 0) {
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
       

      } else if (res.data.code == -1) {
        console.log("保存错误")
        wx.showModal({
          title: '提示',
          content: '提交错误',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')

            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },
    fail: function () {
    },
    complete: function () {
    }
  })
}